package edu.ynu.se.xiecheng.achitectureclass.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.dao.DeviceDao;
import edu.ynu.se.xiecheng.achitectureclass.dto.DeviceDTO;
import edu.ynu.se.xiecheng.achitectureclass.entity.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeviceService extends LogicService<DeviceDao, Device, Long> {

    public DeviceService(@Autowired DeviceDao deviceDao) {
        super(deviceDao);
    }

    public List<DeviceDTO> getAllDevicesDTO() {
        List<Device> devices = getAll();
        return devices.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    public DeviceDTO getDeviceDTO(Long id) {
        Device device = getDAO().getById(id);
        return convertToDTO(device);
    }

    private DeviceDTO convertToDTO(Device device) {
        DeviceDTO deviceDTO = new DeviceDTO();
        deviceDTO.setId(device.getId());
        deviceDTO.setName(device.getName());
        deviceDTO.setSerialNumber(device.getSerialNumber());
        deviceDTO.setDeviceType(device.getDeviceType());
        deviceDTO.setUnit(device.getUnit());
        deviceDTO.setMonitoringAddress(device.getMonitoringAddress());
        deviceDTO.setGreenhouseId(device.getGreenhouseId());
        deviceDTO.setCreatedAt(device.getCreatedAt());
        return deviceDTO;
    }
}
